package com.song.controller.user;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.common.Result;
import com.song.entity.user.User;
import com.song.service.user.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("${api.version}/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 跳转到用户列表界面
     */
    @RequestMapping("/userManage")
    public String userManage(){
        return "user/userManage";
    }

    /**
     * 获取用户列表
     */
    @RequestMapping("/userList")
    @ResponseBody
    public Result<List<User>> userList(
            String account, String name, String phone,
            @RequestParam(value = "page", defaultValue = "0") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Result<List<User>> result  =  userService.queryUserList(account,name,phone,pageNum,pageSize);
        return result;
    }

    /**
     *  根据用户ID删除用户
     */
    @RequestMapping("/deleteUserById")
    @ResponseBody
    public Result<Integer>  deleteUserById(int id) {
        Result<Integer> result =  userService.deleteUserById(id);
        return result;
    }

    /**
     * 新增用户
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public Result<Integer> addUser(User user) {
        Result<Integer> result =  userService.addUser(user);
        return result;
    }


    /**
     * 批量删除用户
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Result<Integer> batchDel(String ids) {
        String[] array = ids.split(",");
        Result<Integer> result =  userService.batchDel(array);
        return result;
    }

    /**
     * 更新用户名
     */
    @RequestMapping("/updateUserName")
    @ResponseBody
    public Result<Integer> updateUserName(String name,int id) {
        Result<Integer> result =  userService.updateUserName(name,id);
        return result;
    }

    /**
     * 更新用户
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public Result<Integer> updateUser(User user) {
        Result<Integer> result =  userService.updateUser(user);
        return result;
    }

    @RequestMapping("/selectUserList")
    @ResponseBody
    public String selectUserList(@RequestParam(value = "page", defaultValue = "1") int pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userService.selectList(new EntityWrapper<User>());

        PageInfo<User> pageInfo = new PageInfo<>(list);

        JSONObject json = new JSONObject();
        json.put("page",pageNum);
        json.put("list",list);
        json.put("totalPage", pageInfo.getPages());
        json.put("pageSize",pageSize);

        return json.toString();
    }
}
